package com.msmall.community.album.action.html;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.community.album.model.Album;
import com.msmall.community.album.service.AlbumManager;

public class AlbumAction extends HTMLBaseAction{

	private static final long serialVersionUID = -1936710450518300590L;

	private static final Log logger = LogFactory.getLog(AlbumAction.class);
	
	protected AlbumManager albumManager;

	protected Long albumid;
	protected Long photoid;
	/**
	 * 删除相册时，相片被移动到
	 */
	protected Long targetid;
	
	protected String password;
	
	protected boolean isOwner;
	
	protected Album album;
	
	protected List<Album> albums;
	
	/**
	 * 进入创建相册页面
	 * @return
	 */
	public String create(){
		album = null;
		return SUCCESS;
	}
	/**
	 * 保存创建的相册
	 * @return
	 */
	public String save(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try {
			albumManager.saveAlbum(album, getUserid());
			buffer.append(createResult("true"));
		} catch (Exception e) {
			buffer.append(createResult("false"));
			logger.debug(e);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	public String update(){
		try{
			album = (Album) albumManager.get(Album.class, albumid);
		}catch(Exception e){
			logger.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 删除相册
	 * @return
	 */
	public String remove(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			if(targetid != null && targetid >0){
				albumManager.removeAlbum(albumid, targetid, getUserid());
				buffer.append(createResult("true"));
			}else{
				album = (Album) albumManager.get(Album.class, albumid);
				if(album.getCounts() > 0){
					albums = albumManager.getAlumsByUserid(album.getUserid());
					return "transfer";
				}else{
					albumManager.removeAlbum(albumid, targetid, getUserid());
					buffer.append(createResult("true"));
				}
			}
		} catch (Exception e) {
			buffer.append(createResult("false"));
			logger.debug(e);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	/**
	 * 分页显示一个相册下的相片
	 */
	public String list(){
		try {
			album = (Album) albumManager.get(Album.class, albumid);
			if(album.getUserid().compareTo(getUserid()) == 0){
				isOwner = true;
			}else{
				isOwner = false;
			}
			albumManager.albumList(page, getUserid(), albumid);
		} catch (Exception e) {
			logger.debug(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 访客进入加密相册时，弹出输入密码页面
	 * @return
	 */
	public String goValid(){
		return SUCCESS;
	}
	/**
	 * 访客进入加密相册时，验证密码
	 * @return
	 */
	public String valid(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			if(albumManager.valid(albumid, getUserid(), password)){
				buffer.append(createResult("true"));
			}else{
				buffer.append(createResult("false"));
			}
		} catch (Exception e) {
			buffer.append(createResult("false"));
			logger.debug(e);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	public String setCover(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			albumManager.setCover(albumid, photoid, getUserid());
			buffer.append(createResult("true"));
		} catch (Exception e) {
			buffer.append(createResult("false"));
			logger.debug(e);
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	public AlbumManager getAlbumManager() {
		return albumManager;
	}
	public void setAlbumManager(AlbumManager albumManager) {
		this.albumManager = albumManager;
	}
	public Long getAlbumid() {
		return albumid;
	}
	public void setAlbumid(Long albumid) {
		this.albumid = albumid;
	}
	public Long getPhotoid() {
		return photoid;
	}
	public void setPhotoid(Long photoid) {
		this.photoid = photoid;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Album getAlbum() {
		return album;
	}
	public void setAlbum(Album album) {
		this.album = album;
	}
	public List<Album> getAlbums() {
		return albums;
	}
	public void setAlbums(List<Album> albums) {
		this.albums = albums;
	}
	public boolean getIsOwner() {
		return isOwner;
	}
	public void setIsOwner(boolean isOwner) {
		this.isOwner = isOwner;
	}
	public Long getTargetid() {
		return targetid;
	}
	public void setTargetid(Long targetid) {
		this.targetid = targetid;
	}
}
